<?php

namespace Apps\Common\Libs;


use AMap\CoordType;
use AMap\LBSCloud;
use Apps\Common\Models\BeautyParlor;
use Apps\Common\Models\BeautyParlorAmap;
use Phalcon\Db\RawValue;
use PhalconPlus\Common\Component;

/**
 * 同步地理数据到地图云存储
 * @package Apps\Common\Libs
 */
class BeautyParlorSyncLocation extends Component
{
    public function sync($id)
    {
        $config = $this->di->get('appConfig')->amap->lbs_cloud;
        $key = $config->key;
        $signKey = $config->sign_key;
        $tableId = $config->table_id;
        $syncLogTableName = 'beauty_parlor_amap_sync_log';
        $lbsColud = new LBSCloud($key, $signKey, $tableId, $syncLogTableName);
        // 获取店铺信息
        $shop = BeautyParlor::query()
            ->columns([
                'bp_id',
                'bp_name',
                'bp_address',
                'bp_cover',
                'bp_tel',
                'bp_medal',
                'X(bp_coordinate) x',
                'Y(bp_coordinate) y',
            ])
            ->where('bp_id=:id:', ['id'=>$id])
            ->execute()
            ->getFirst();
        if(!$shop) {
            return false;
        }
        $name = $shop->bp_name;
        $location = $shop->x . ',' . $shop->y;
        $address = $shop->bp_address;
        $customData = [
            //'_image' => PicUrl::BeautyParlor($shop->bp_cover, $this->di),
            'bp_id' => $shop->bp_id,
            'bp_cover' => PicUrl::BeautyParlorCover($shop->bp_cover, $this->di),
            'tel' => $shop->bp_tel,
            'bp_tags' => implode(',', \Apps\Common\Libs\BeautyParlor::getTags($id)),
            'medal_refund' => \Apps\Common\Libs\BeautyParlor::getMedalRefund($shop->bp_medal),
            'medal_verify' => \Apps\Common\Libs\BeautyParlor::getMedalVerify($shop->bp_medal),
        ];
        // 获取已经同步的数据
        $aMapData = BeautyParlorAmap::findFirst("source_id=$id");
        if($aMapData) {
            // 更新
            $result = $lbsColud->update($id, $aMapData->amap_data_id, $name, $location, $address, $customData, CoordType::AUTONAVI);
            if($result->status) {
                // 记录更新时间
                $aMapData->sync_time = new RawValue('NOW()');
                if($aMapData->save() == false) {
                    $this->logDatabaseError($aMapData);
                }
            }
        } else {
            // 新添加
            $result = $lbsColud->create($id, $name, $location, $address, $customData, CoordType::AUTONAVI);
            if($result->status) {
                // 记录关系数据
                $aMapData = new BeautyParlorAmap();
                $aMapData->source_id = $id;
                $aMapData->amap_data_id = $result->id;
                $aMapData->sync_time = new RawValue('NOW()');
                if($aMapData->save() == false) {
                    $this->logDatabaseError($aMapData);
                }
            }
        }
        return $result->status;
    }

    /**
     * 删除
     * @param $id
     * @return bool
     */
    public function delete($id)
    {
        $config = $this->di->get('appConfig')->amap->lbs_cloud;
        $key = $config->key;
        $signKey = $config->sign_key;
        $tableId = $config->table_id;
        $syncLogTableName = 'beauty_parlor_amap_sync_log';
        $lbsColud = new LBSCloud($key, $signKey, $tableId, $syncLogTableName);
        // 获取已经同步的数据
        $aMapData = BeautyParlorAmap::findFirst("source_id=$id");
        if($aMapData) {
            $result = $lbsColud->delete($id, $aMapData->amap_data_id);
            $aMapData->delete();
        }
        return false;
    }
}